package com.aso114.project.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.bean.News;
import com.aso114.project.bean.TrueQuestionBean;

import java.util.List;

public class SatPractieAdapter extends  RecyclerView.Adapter<SatPractieAdapter.MyViewHolder> {

    private List<TrueQuestionBean> mDatas;
    private Context mContext;
    private LayoutInflater inflater;

    private OnItemClickListener mClickListener;
    public SatPractieAdapter(Context context, List<TrueQuestionBean> datas){
        this. mContext=context;
        this. mDatas=datas;
        inflater=LayoutInflater. from(mContext);
    }

    @Override
    public int getItemCount() {

        return mDatas.size();
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv.setText( mDatas.get(position).getPaperTitle());
        if(mDatas.get(position).getUserCount().equals("")){
            holder.Score.setText( "已经做过0次");
        }else{
            holder.Score.setText( "已经做过"+mDatas.get(position).getUserCount()+"次");
        }
        if(mDatas.get(position).getExamUserCount().equals("")){
            holder.ExamDuration.setText( "0人已做:");
        }else{
            holder.ExamDuration.setText( mDatas.get(position).getExamUserCount()+"人已做");
        }

        holder.Difficulty.setText("难度:"+ mDatas.get(position).getDifficultyFactor());
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.satpractice_item,parent, false);
        MyViewHolder holder= new MyViewHolder(view,mClickListener);
        return holder;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    class MyViewHolder extends ViewHolder implements View.OnClickListener {

        TextView tv;
        TextView Difficulty;
        TextView ExamDuration;
        TextView Score;
        private OnItemClickListener mListener;// 声明自定义的接口
        public MyViewHolder(View itemView, OnItemClickListener listener) {
            super(itemView);
            tv=(TextView) itemView.findViewById(R.id. tv_item);
            Difficulty =(TextView) itemView.findViewById(R.id. DifficultyFactor);
            ExamDuration =(TextView) itemView.findViewById(R.id. ExamDuration);
            Score = (TextView) itemView.findViewById(R.id. Score);
            mListener = listener;
            // 为ItemView添加点击事件
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mListener.onItemClick(v, getPosition());
        }
    }



    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

}

